<?if(!defined("PROLOG_INCLUDED") || PROLOG_INCLUDED!==true)die();

class Parser {
	public $pageUrl = "";
	public $pagePath = "";

	public $arPageParams = Array();
	public $arSystemTags = Array();
	public $arComponents = Array();

	public $arPlaceHolders = Array();

	public $cPhpOpenTags = 0;
	public $cPhpCloseTags = 0;

	private $arSysTags = Array(
		'/^\<\?{0,1}\ {0,5}require_once\ {0,5}\$\_SERVER\[\"DOCUMENT_ROOT\"\]\ {0,5}\.\ {0,5}(\"|\')\/system\/core\/(prolog.php|epilog.php)(\"|\');{0,1}\?\>{0,1}/i',
		'/^(\<\?){0,1}\ {0,5}define\((\"|\')(.*)(\"|\')\,\ {0,5}true\);{0,1}\ {0,5}(\?\>){0,1}/i',
		
	);

	private $arPageParamTags = Array(
		'/^(\<\?){0,1}\ {0,5}SetTitle\((\"|\')(.*)(\"|\')\);{0,1}\ {0,5}(\?\>){0,1}/i',
	);

	public $arPageLines = Array();
	public $pageContentOrig = "";
	public $pageContent = "";
	public $fullPageText = "";

	private $cSysTags = 0;
	private $cPageParamTags = 0;
	private $cComponentTags = 0;


	private $beforePH = '#';
	private $afterPH = '#';

	public function __construct($pageUrl = false) {
		$_pageUrl = $pageUrl === false ? $_SERVER['PHP_SELF'] : $pageUrl;
		if (is_file($_SERVER['DOCUMENT_ROOT'].$_pageUrl)) {
			$this->pageUrl = $_pageUrl;
			$this->pagePath = $_SERVER['DOCUMENT_ROOT'].$_pageUrl;
			$this->fullPageText = file_get_contents($this->pagePath);
			$this->pageContentOrig = $this->fullPageText;
			$this->pageContent = $this->fullPageText;
			$this->arPageLines = explode("\n", $this->fullPageText);

			if (!empty($this->arPageLines)) {
				$this->arSystemTags = $this->ParseSysTags();
				$this->arPageParams = $this->ParsePageParams();
				$this->arComponents = $this->ParseComponents();
				
				$this->pageContentOrig = $this->pageContent;
				$this->pageContent = $this->ParseText();

				$this->pageContentOrder = str_replace($this->pageContent,'#PARSER_CONTENT#', $this->pageContentOrig);
			}

		} else {
			return null;
		}
	}

	public function Match($string, $arPatterns = Array()) {
		if (!empty($arPatterns)) {
			foreach ($arPatterns as $pattern) {
				if (preg_match($pattern, $string)) {
					return true;
				}
			}
		}
		return false;
	}

	public function CountPhpTags() {
		
		
	}

	public function GetHtml() {
		$html = $this->pageContent;
		return $html;
	}

	public function ParseSysTags() {
		$arResult = Array();
		foreach ($this->arPageLines as $line) {
			if (self::Match(trim($line), $this->arSysTags)) {
				$arResult[] = trim($line);
				$tagName = "PARSER_SYSTEM_TAG_{$this->cSysTags}";
				$this->arPlaceHolders[$tagName] = trim($line);
				$this->pageContent = str_replace($line, $this->beforePH.$tagName.$this->afterPH, $this->pageContent);
				$this->cSysTags++;
			}
		}
		return $arResult;
	}

	public function ParsePageParams() {
		$arResult = Array();
		foreach ($this->arPageLines as $line) {
			if (self::Match(trim($line), $this->arPageParamTags)) {
				$arResult[] = trim($line);
				$tagName = "PARSER_PAGE_PARAM_TAG_{$this->cPageParamTags}";
				$this->arPlaceHolders[$tagName] = trim($line);
				$this->pageContent = str_replace($line, $this->beforePH.$tagName.$this->afterPH, $this->pageContent);
				$this->cPageParamTags++;
			}
		}
		return $arResult;
	}

	public function ParsePhpTags() {
		$arResult = Array();

		return $arResult;
	}

	public function ParseComponents() {
		$arResult = Array();
		$pattern =  '/(\<\?){0,1}\ {0,5}Component::getComponent\((\"|\')'.
			'([a-zA-Z0-9\_\-\.]+)\:([a-zA-Z0-9\_\-\.]+)(\"|\')'.
			'\,\ {0,5}(\"|\')([a-zA-Z0-9\_\-\.]+)(\"|\')\,\ {0,5}'.
			'Array\(([a-zA-Z0-9\"\'\.\,\_\-\ \/\\\(\)\=\>\$\[\]\?\<\;\n\r\t.+]*)(\?\>){0,1}$/im';
		
			if (preg_match($pattern, $this->pageContent, $matches)) {
				$arResult[] = trim($matches[0]);
				$tagName = "PARSER_COMPONENT_TAG_{$this->cComponentTags}";
				$this->arPlaceHolders[$tagName] = trim($matches[0]);
				$this->pageContent = str_replace($matches[0], $this->beforePH.$tagName.$this->afterPH, $this->pageContent);
				$this->cComponentTags++;
			}
		//}
		return $arResult;
	}

	public function ParseText() {
		$result = $this->pageContent;

		foreach ($this->arPlaceHolders as $PHK => $PHV) {
			$result = str_replace($this->beforePH.$PHK.$this->afterPH, '', $result);
		}
		
		return $result;
	}
	
	public function debug($str) {
		return nl2br(htmlspecialcharsEx($str));
	}
}
?>